﻿using System.Collections.Generic;
using TamoCRM.Call;
using TamoCRM.Core.Caching;
using TamoCRM.Core.Commons.Extensions;
using TamoCRM.Domain.Branches;
using TamoCRM.Domain.CallHistories;
using TamoCRM.Domain.Channels;
using TamoCRM.Domain.Collectors;
using TamoCRM.Domain.EducationLevels;
using TamoCRM.Domain.Groups;
using TamoCRM.Domain.Levels;
using TamoCRM.Domain.Locations;
using TamoCRM.Domain.Majors;
using TamoCRM.Domain.Phones;
using TamoCRM.Domain.Schools;
using TamoCRM.Domain.SourceTypeMap;
using TamoCRM.Domain.SourceTypes;
using TamoCRM.Domain.Status;
using TamoCRM.Domain.StatusCare;
using TamoCRM.Domain.StatusMap;
using TamoCRM.Domain.UserRole;
using TamoCRM.Services.Branches;
using TamoCRM.Services.CallHistories;
using TamoCRM.Services.Channels;
using TamoCRM.Services.Collectors;
using TamoCRM.Services.Contacts;
using TamoCRM.Services.EducationLevels;
using TamoCRM.Services.Groups;
using TamoCRM.Services.Levels;
using TamoCRM.Services.Locations;
using TamoCRM.Services.Majors;
using TamoCRM.Services.Phones;
using TamoCRM.Services.Schools;
using TamoCRM.Services.SourceTypeMap;
using TamoCRM.Services.SourceTypes;
using TamoCRM.Services.Status;
using TamoCRM.Services.StatusCare;
using TamoCRM.Services.StatusMap;
using TamoCRM.Services.UserRole;
using System.Linq;

namespace TamoCRM.Web.Framework
{
    public class StoreData
    {
        public static List<UserInfo> ListUser = new List<UserInfo>();
        public static List<GroupInfo> ListGroup = new List<GroupInfo>();
        public static List<LevelInfo> ListLevel = new List<LevelInfo>();
        public static List<MajorInfo> ListMajor = new List<MajorInfo>();
        public static List<BranchInfo> ListBranch = new List<BranchInfo>();
        public static List<SchoolInfo> ListSchool = new List<SchoolInfo>();
        public static List<StatusInfo> ListStatus = new List<StatusInfo>();
        public static List<ChannelInfo> ListChannel = new List<ChannelInfo>();
        public static List<LocationInfo> ListLocation = new List<LocationInfo>();
        public static List<CollectorInfo> ListCollector = new List<CollectorInfo>();
        public static List<StatusMapInfo> ListStatusMap = new List<StatusMapInfo>();
        public static List<StatusCareInfo> ListStatusCare = new List<StatusCareInfo>();
        public static List<SourceTypeInfo> ListSourceType = new List<SourceTypeInfo>();
        public static List<SourceTypeMapInfo> ListSourceTypeMap = new List<SourceTypeMapInfo>();
        public static List<EducationLevelInfo> ListEducationLevel = new List<EducationLevelInfo>();

        public static void LoadData()
        {
            ListUser = UserRepository.GetAll() ?? new List<UserInfo>();
            ListGroup = GroupRepository.GetAll() ?? new List<GroupInfo>();
            ListLevel = LevelRepository.GetAll() ?? new List<LevelInfo>();
            ListMajor = MajorRepository.GetAll() ?? new List<MajorInfo>();
            ListBranch = BranchRepository.GetAll() ?? new List<BranchInfo>();
            ListSchool = SchoolRepository.GetAll() ?? new List<SchoolInfo>();
            ListStatus = StatusRepository.GetAll() ?? new List<StatusInfo>();
            ListChannel = ChannelRepository.GetAll() ?? new List<ChannelInfo>();
            ListLocation = LocationRepository.GetAll() ?? new List<LocationInfo>();
            ListCollector = CollectorRepository.GetAll() ?? new List<CollectorInfo>();
            ListStatusMap = StatusMapRepository.GetAll() ?? new List<StatusMapInfo>();
            ListStatusCare = StatusCareRepository.GetAll() ?? new List<StatusCareInfo>();
            ListSourceType = SourceTypeRepository.GetAll() ?? new List<SourceTypeInfo>();
            ListSourceTypeMap = SourceTypeMapRepository.GetAll() ?? new List<SourceTypeMapInfo>();
            ListEducationLevel = EducationLevelRepository.GetAll() ?? new List<EducationLevelInfo>();
        }

        public static void LoadDataUser()
        {
            ListUser = UserRepository.GetAll() ?? new List<UserInfo>();
        }

        public static string GetAllMajor()
        {
            return ListMajor
                .Where(c => c.MajorId != 1)
                .Select(c => c.MajorId.ToString())
                .Aggregate((total, curent) => total + "," + curent);
        }

        public static void LoadRedis(int contactId)
        {
            if (contactId == 0) return;
            var lstPhones = PhoneRepository.GetByContact(contactId) ?? new List<PhoneInfo>();
            foreach (var phone in lstPhones.Where(c => !string.IsNullOrEmpty(c.PhoneNumber)))
                CachingProvider.Instance().Set(phone.PhoneNumber, phone.ContactId.ToString());

            var contactInfo = ContactRepository.GetInfo(contactId);
            if (contactInfo != null)
            {
                if (!contactInfo.Email.IsStringNullOrEmpty())
                    CachingProvider.Instance().Set(contactInfo.Email, contactInfo.Id.ToString());
                if (!contactInfo.Email2.IsStringNullOrEmpty())
                    CachingProvider.Instance().Set(contactInfo.Email2, contactInfo.Id.ToString());
            }
        }

        public static void DeleteRedis(int contactId)
        {
            var phones = PhoneRepository.GetByContact(contactId) ?? new List<PhoneInfo>();
            var contactInfo = ContactRepository.GetInfo(contactId);

            // Redis
            foreach (var item in phones) CachingProvider.Instance().Del(item.PhoneNumber);
            if (contactInfo != null)
            {
                CachingProvider.Instance().Del(contactInfo.Email);
                CachingProvider.Instance().Del(contactInfo.Email2);
            }
        }

        public static void DeleteRedis(string mobile1, string mobile2, string tel, string email, string email2, int contactId = 0)
        {
            CachingProvider.Instance().Del(tel);
            CachingProvider.Instance().Del(email);
            CachingProvider.Instance().Del(email2);
            CachingProvider.Instance().Del(mobile1);
            CachingProvider.Instance().Del(mobile2);
            LoadRedis(contactId);
        }
        
        public static CallHistoryInfo WsUpdateCallHistoryInfo(int callHistoryId)
        {
            if (callHistoryId.IsIntegerNull()) return null;
            var entity = CallHistoryRepository.GetInfo(callHistoryId);
            return WsUpdateCallHistoryInfo(entity);
        }

        public static CallHistoryInfo WsUpdateCallHistoryInfo(CallHistoryInfo entity)
        {
            if (entity.CallId.IsStringNullOrEmpty() || entity.CallId == "-1") return entity;
            var item = HelpUtils.GetCall(entity.CallId, entity.MobilePhone, entity.StationId, entity.AgentCode);
            if (item == null) return entity;

            entity.Status = item.status;
            entity.CallId = item.call_id;
            entity.Duration = item.duration;
            entity.RingTime = item.ringtime;
            entity.StationId = item.station_id;
            entity.AgentCode = item.agent_code;
            entity.ErrorCode = item.error_code;
            entity.ErrorDesc = item.error_desc;
            entity.MessageCode = item.message_code;
            entity.MobilePhone = item.mobile_phone;
            entity.LinkRecord = item.link_down_record.IsStringNullOrEmpty()
                                    ? string.Empty
                                    : item.link_down_record.Replace("/var/spool/asterisk/monitor/Recordings", "http://203.162.121.75/recordings/Recordings");
            entity.EndTime = item.end_time.ToDateTime("yyyyMMddHHmmss");
            entity.StartTime = item.start_time.ToDateTime("yyyyMMddHHmmss");
            entity.ResponseTime = item.datetime_response.ToDateTime("yyyyMMddHHmmss");
            return entity;
        }
    }
}